﻿namespace CraftingCode.SymbolicCalculator
{
    using System;

    public static class ExpressionExtensions
    {
        public static string RemoveFirstInstanceOf(this string expression, Token token)
        {
            var indexOfToken = expression.IndexOf(token.Value, System.StringComparison.InvariantCultureIgnoreCase);
            return expression.Substring(indexOfToken + token.Value.Length);
        }

        public static bool HasContent(this string expression)
        {
            return !string.IsNullOrWhiteSpace(expression);
        }       

        public static string ReadCharactersWhile(this string expression, Func<char, int, bool> shouldContinue)
        {
            var value = string.Empty;
            var index = 0;
            foreach (var c in expression)
            {
                if (shouldContinue(c, index))
                {
                    value += c;
                }
                else
                {
                    break;
                }

                index++;
            }

            return value;
        }
    }
}